home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue32 / forms / NDXDemos / m1main.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-02-22  |  2.7 KB  |  124 lines

  1. unit M1Main;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, Buttons, ExtCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Panel1: TPanel;
  12.     Label2: TLabel;
  13.     Label3: TLabel;
  14.     Bevel1: TBevel;
  15.     BitBtn2: TBitBtn;
  16.     BitBtn1: TBitBtn;
  17.     BitBtn4: TBitBtn;
  18.     Label1: TLabel;
  19.     ListBox1: TListBox;
  20.     Label5: TLabel;
  21.     OpenDialog1: TOpenDialog;
  22.     Label4: TLabel;
  23.     procedure BitBtn2Click(Sender: TObject);
  24.     procedure BitBtn1Click(Sender: TObject);
  25.     procedure BitBtn4Click(Sender: TObject);
  26.   private
  27.     { Private declarations }
  28.   public
  29.     { Public declarations }
  30.   end;
  31.  
  32. var
  33.   Form1: TForm1;
  34.  
  35. implementation
  36.  
  37. uses Prevform;
  38.  
  39. {$R *.DFM}
  40.  
  41. procedure TForm1.BitBtn2Click(Sender: TObject);
  42. begin
  43.   if OpenDialog1.Execute then
  44.     Label3.Caption := OpenDialog1.FileName;
  45. end;
  46.  
  47. procedure TForm1.BitBtn1Click(Sender: TObject);
  48. type
  49.   PBuffer = ^TBuffer;
  50.   TBuffer = array[0..65200] of char;
  51. var
  52.   Form: file;
  53.   Index: file of LongInt;
  54.   i: LongInt;
  55.   Buf: PBuffer;
  56. begin
  57.   { assign and open selected file }
  58.   AssignFile(Form, Label3.Caption);
  59.   Reset(Form, 1);
  60.   try
  61.     { assign and create index file }
  62.     AssignFile(Index, ChangeFileExt(Label3.Caption, '.NDX'));
  63.     Rewrite(Index);
  64.     { getmem for buffer Note: you would need to page
  65.       files bigger than 64K if using Delphi 1}
  66.     GetMem(Buf, FileSize(Form));
  67.     try
  68.       { read form into buffer }
  69.       BlockRead(Form, Buf^, FileSize(Form));
  70.       for i := 0 to FileSize(Form) do
  71.         if Buf^[i] = '@' then { found marker }
  72.         begin
  73.           Write(Index, i); { write offset to index }
  74.           ListBox1.Items.Add(IntToStr(i)); { display offset for user }
  75.         end;
  76.     finally
  77.       { clean up }
  78.       FreeMem(Buf, FileSize(Form));
  79.     end;
  80.   finally
  81.     { clean up }
  82.     CloseFile(Form);
  83.     CloseFile(Index)
  84.   end;
  85. end;
  86.  
  87. procedure TForm1.BitBtn4Click(Sender: TObject);
  88. var
  89.   i, j: integer;
  90.   F: TFileStream;
  91.   Index: file of integer;
  92.   S: string;
  93. begin
  94.   { open form file }
  95.   F := TFileStream.Create(Label3.Caption, fmOpenWrite);
  96.   try
  97.     { assign and open index file }
  98.     AssignFile(Index, ChangeFileExt(Label3.Caption, '.NDX'));
  99.     Reset(Index);
  100.     try
  101.       { iterate through index offsets }
  102.       for j := 0 to FileSize(Index)-1 do
  103.       begin
  104.         Read(Index, i);
  105.         { seek to offset }
  106.         F.Seek(i, 0);
  107.         S := Format('%10s', ['Offset '+IntToStr(i)]);
  108.         { write data to stream }
  109.         F.Write(S[1], Length(S));
  110.       end;
  111.     finally
  112.       CloseFile(Index);
  113.     end;
  114.   finally
  115.     { clean up }
  116.     F.Free;
  117.   end;
  118.   { show file in memo }
  119.   CCForm.FileToShow := Label3.Caption;
  120.   CCForm.Show;
  121. end;
  122.  
  123. end.
  124.